package com.example.teacher

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
import com.example.teacher.Fragment.CourseFragment
import com.example.teacher.Fragment.ListFragment
import com.example.teacher.Fragment.MainFragment
import com.example.teacher.adapter.DrawerAdapter
import com.example.teacher.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
//    private lateinit var mDrawerToggle: ActionBarDrawerToggle

    private lateinit var adapter:DrawerAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)


        //Log.d("res", ""+res.toString())


        initView()

        initListener()

//        binding.list.setOnClickListener{
//            val data= OkHttp.getSync("${BaseUrl.URL}getList")?:return@setOnClickListener
//            val intent=Intent(this,ListActivity::class.java)
//            intent.putExtra("data",data.toString())
//            startActivity(intent)
//        }
//
//        binding.add.setOnClickListener{
//            startActivity(Intent(this,AddActivity::class.java))
//        }

    }

    private fun initView() {

        binding.toolbar.setTitle("首页")
        binding.toolbar.setLogo(R.drawable.ic_toolbar_logo)
        setSupportActionBar(binding.toolbar)

        adapter=DrawerAdapter()

        binding.leftDrawer.adapter=adapter

        adapter.setSelectedItem(0)
        supportFragmentManager.beginTransaction()
            .replace(binding.contentFrame.id, MainFragment()).commit()

        //初使化侧边栏监听事件
//        mDrawerToggle = ActionBarDrawerToggle(this, binding.drawerLayout, 0, 0)
//        mDrawerToggle.syncState()
//        //绑定侧边栏监听事件
//        binding.drawerLayout.addDrawerListener(mDrawerToggle)

//        //侧边栏菜单图标颜色恢复正常，而非默认的灰色
//        binding.navigation.itemIconTintList = null
//        //绑定侧边栏菜单点击事件
//        binding.navigation.setNavigationItemSelectedListener { item: MenuItem ->
//            when (item.itemId) {
//                R.id.item_index -> supportFragmentManager.beginTransaction()
//                    .replace(R.id.frameLayout, MainFragment()).commit()
//
//                R.id.item_attendance -> supportFragmentManager.beginTransaction()
//                    .replace(R.id.frameLayout, ListFragment()).commit()
//
//            }
//            true
//        }
//        supportFragmentManager.beginTransaction().replace(R.id.frameLayout, MainFragment()).commit()
    }

    private fun initListener(){

        binding.toolbar.setOnClickListener {
            binding.drawerLayout.openDrawer(GravityCompat.START)
        }

        binding.leftDrawer.setOnItemClickListener(object :AdapterView.OnItemClickListener{
            override fun onItemClick(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                when(position){
                    0->{
                        supportFragmentManager.beginTransaction()
                            .replace(binding.contentFrame.id, MainFragment()).commit()
                        binding.toolbar.setTitle("首页")
                    }
                    1->{
                        supportFragmentManager.beginTransaction()
                            .replace(binding.contentFrame.id, CourseFragment()).commit()
                        binding.toolbar.setTitle("课程")
                    }

                    2->{
                        supportFragmentManager.beginTransaction()
                            .replace(binding.contentFrame.id, ListFragment()).commit()
                        binding.toolbar.setTitle("考勤历史")
                    }
                }
            }

        })

    }
}